package com.jjb.common.view.draw;

import com.jjb.common.view.DrawUtil;
import com.jjb.common.view.model.Table;
import com.jjb.common.view.model.TableRow;
import sun.font.FontDesignMetrics;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class TableGrawCanvas implements GrawCanvas {

    /**
     * 表格的起始坐标
     */
    private final int x;
    private final int y;

    private int currentX;

    private int currentY;

    private final Table table;
    private final List<Map<String, Object>> tableContent;

    /**
     * 字体颜色
     */
    private Color color = Color.BLACK;

    /**
     * 图片字体
     */
    private Font font = new Font("微软雅黑", Font.PLAIN,  20);

    // 设置画布的线条
    private BasicStroke basicStroke;


    //是否绘制表头
    private boolean isHeader = Boolean.TRUE;


    public boolean isHeader() {
        return isHeader;
    }

    public void setHeader(boolean header) {
        isHeader = header;
    }

    public TableGrawCanvas(int x, int y, Table table, List  tableContent) {
        this.x = x;
        this.y = y;
        this.table = table;
        this.tableContent = tableContent;
    }

    public BasicStroke getBasicStroke() {
        return basicStroke;
    }

    public void setBasicStroke(BasicStroke basicStroke) {
        this.basicStroke = basicStroke;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public Font getFont() {
        return font;
    }

    public void setFont(Font font) {
        this.font = font;
    }



    private static int getWidth(Font font, String content) {
        FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
        int width = 0 ;
        for(int i =0; i < content.length(); ++i) {
            width += metrics.charWidth(content.charAt(i));
        }
        return width;
    }

    public int getTableHeight() {
        int height = 0;
        if( isHeader ) {
            int minHeight = 0 ;
            for(int i = 0; i < table.getTableRows().size(); ++i ) {
                TableRow row = table.getTableRows().get(i);
                minHeight = tableRowNodeMinHeight(table.getSpace(), table.getHeadFont(),row.getWidth() - row.getPadding() * 2, table.getRowSpacing(), minHeight, row.getHeadName());
            }
            height += minHeight;
        }

        for( Map<String, Object> obj :  tableContent ) {
            int minHeight = 0;
            for (int i = 0; i < table.getTableRows().size(); ++i) {
                TableRow row = table.getTableRows().get(i);
                String content = obj.get(row.getProperty()).toString();
                minHeight = tableRowNodeMinHeight(table.getSpace(), row.getFont(), row.getWidth() - row.getPadding() * 2, table.getRowSpacing(),
                        Math.max(minHeight, row.getHeight())
                        , content);
            }
            height += minHeight;
        }

        return height;
    }


    public static void main(String[] args) throws Exception{
        File srcImgFile = new File("C:\\Users\\wcj\\Downloads\\16916594310144902.jpg");//得到文件
        Image srcImg = ImageIO.read(srcImgFile);//文件转化为图片
        int srcImgWidth = srcImg.getWidth(null);//获取图片的宽
        int srcImgHeight = srcImg.getHeight(null);//获取图片的高
        // 加水印
        BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bufImg.createGraphics();
        g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);

        DrawUtil.drawRectangle(g, Color.BLACK,200, 200, 80, 50);

        g.dispose();

        // 输出图片
        FileOutputStream outImgStream = new FileOutputStream("C:\\Users\\wcj\\Desktop\\Z123.jpg");
        ImageIO.write(bufImg, "jpg", outImgStream);
        outImgStream.flush();
    }

    private int tableRowNodeMinHeight(int space, Font font, int width, int rowSpacing, int minHeight, String ... contents) {
        for( String content : contents ) {
            int height = DrawUtil.getHeight(font, content, width, space, rowSpacing);
            minHeight = Math.max(minHeight, height);
        }
        return minHeight;
    }

    private void drawTableRowNodeContent(Graphics2D g, int x, int y,int space, Font font, Color color, String content, int width,int height, int rowSpacing, int textAlign) {
        //判断是否需要换行
        ArrayList<String> contents = DrawUtil.getWidthLine(font, content, width, space);
        if( contents== null || contents.size() <= 1 ) {
            int contentWidth = DrawUtil.getWidth(font, content, space);
            int cx = x + (width - contentWidth)/2;
            int cy = y + height/2 + font.getSize()/2;
            DrawUtil.drawText(g, textAlign == 1 ? x : cx, cy, color, font,space,  content);
        } else {
            int cy = y +  font.getSize();
            DrawUtil.drawTextArea(g, color, font, x, cy,width, rowSpacing, space, content);
        }
    }

    public void draw(Graphics2D g) {
        if(this.basicStroke != null) {
            g.setStroke(basicStroke);
        }

        //开始画头部
        int currentX = this.x;
        int currentY = this.y;

        if ( isHeader ) {

            int minHeight = 0 ;
            for(int i = 0; i < table.getTableRows().size(); ++i ) {
                TableRow row = table.getTableRows().get(i);
                minHeight = tableRowNodeMinHeight(table.getSpace(), table.getHeadFont(),row.getWidth() - row.getPadding() * 2, table.getRowSpacing(), minHeight, row.getHeadName());
                //minHeight = minHeight + row.getPadding() * 2;
            }


            //开始绘制头部
            for(int i = 0; i < table.getTableRows().size(); ++i ) {
                TableRow row = table.getTableRows().get(i);
                //绘制一个正方形
                DrawUtil.drawRectangle(g,table.getHeadColor(), currentX , currentY  ,row.getWidth(), minHeight);

                drawTableRowNodeContent(g, currentX + row.getPadding(), currentY + row.getPadding(), table.getSpace(), table.getHeadFont(), table.getHeadColor(), row.getHeadName(), row.getWidth() - row.getPadding() * 2,minHeight - row.getPadding() * 2, table.getRowSpacing(), row.getTextAlign());
                currentX += row.getWidth();
            }
            currentY += minHeight;
        }


        for( Map<String, Object> obj :  tableContent ) {
            currentX = x;

            int minHeight = 0 ;
            for(int i = 0; i < table.getTableRows().size(); ++i ) {
                TableRow row = table.getTableRows().get(i);
                String content = obj.get(row.getProperty()).toString();
                minHeight = tableRowNodeMinHeight(table.getSpace(), row.getFont(), row.getWidth() - row.getPadding() * 2, table.getRowSpacing(),
                        Math.max(minHeight, row.getHeight())
                        , content);
            }

            for(int i = 0; i < table.getTableRows().size(); ++i ) {
                TableRow row = table.getTableRows().get(i);
                String content = obj.get(row.getProperty()).toString();
                //填充一个矩形
                DrawUtil.fillRect(g,row.getBackGroundColor(), currentX , currentY  ,row.getWidth(), minHeight);
                //绘制一个正方形
                DrawUtil.drawRectangle(g,row.getColor(), currentX , currentY  ,row.getWidth(), minHeight);

                drawTableRowNodeContent(g, currentX + row.getPadding(), currentY + row.getPadding(), table.getSpace(), row.getFont(), row.getColor(), content ,row.getWidth() - row.getPadding() * 2,minHeight - row.getPadding() * 2, table.getRowSpacing(), row.getTextAlign());
                currentX += row.getWidth();
            }
            currentY += minHeight;
        }

    }
}
